﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using TTSBLL;
using TTSModel;

public partial class ProductManage_UnitTypeManage : System.Web.UI.Page
{
    private TTSBLL.UnitType unitType;

    public ProductManage_UnitTypeManage()
    {
        unitType = new TTSBLL.UnitType();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindUnitType();
        }
    }

    protected void BindUnitType()
    {
        GridView1.DataSource = unitType.LoadEntities();
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindUnitType();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindUnitType();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        unitType.Delete(id);
        BindUnitType();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindUnitType();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        UnitTypeInfo unitTypeInfo = new UnitTypeInfo();//(GridView1.DataKeys[e.RowIndex].Values[0].ToString(), GridView1.DataKeys[e.RowIndex].Values[1].ToString(), GridView1.DataKeys[e.RowIndex].Values[2].ToString(), GridView1.DataKeys[e.RowIndex].Values[3].ToString());
        unitTypeInfo.ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        unitTypeInfo.Tcode = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        unitTypeInfo.TcodeName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        unitTypeInfo.TcodeVersion = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();

        unitType.Update(unitTypeInfo);
        GridView1.EditIndex = -1;
        BindUnitType();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        UnitTypeInfo unitTypeInfo = new UnitTypeInfo();
        unitTypeInfo.ID = 0;
        unitTypeInfo.Tcode = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        unitTypeInfo.TcodeName = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
        unitTypeInfo.TcodeVersion = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();

        /*UnitTypeInfo pro = unitType.UnitType_GetbyTcode(unitTypeInfo.Tcode);
        if (!string.IsNullOrEmpty(pro.Tcode))
        {
            string str1 = "此Tcode已存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            unitType.Insert(unitTypeInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindUnitType();
        }*/
        unitType.Insert(unitTypeInfo);
        string str1 = "添加成功！";
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        BindUnitType();
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindUnitType();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
